$(function() {
	$('#addForm').submit(function() {
		$.ajax({
			url: '/wms/salmf/addSalmf.xhtml',
			data: $('#addForm').serialize(),
			type: "POST",
			dataType : "text",
			error : function() {
				alert('请求失败!');
			},
			success: function(result) {
				if(result=="success") {
					alert("增加成功!");
					closeDialogAdd();
					reloadTable();
				} else {
					alert(result);
				}
			}
		});
		return false;
	});
	$('#updateForm').submit(function() {
		$.ajax({
			url: '/wms/salmf/updateSalmf.xhtml',
			data: $('#updateForm').serialize(),
			type: "POST",
			dataType : "text",
			error : function() {
				alert('请求失败!');
			},
			success: function(result) {
				if(result=="success") {
					alert("更新成功!");
					closeDialogUpdate();
					reloadTable();
				} else {
					alert(result);
				}
			}
		});
		return false;
	});
	optFormater();
	ajaxTable();
	setDialogAdd();
	closeDialogAdd();
	setDialogUpdate();
	closeDialogUpdate();
});
function ajaxTable() {
	$('#salmfTable').datagrid({
		toolbar : [ {
			id : 'toolbarAdd',
			iconCls : 'icon-add',
			handler : function() {
				if(globalMaps.get('3') != undefined&&(meac=='N'||meac=='S')) {
					openDialogAdd();
				}
			}
		}, '-', {
			id : 'toolbarSearch',
			iconCls : 'icon-remove',
			handler : function() {
				if(globalMaps.get('3') != undefined&&(meac=='N'||meac=='S')) {
					delSalmf();
				}
			}
		}
		],
		frozenColumns:[[
			{field:"ck", checkbox:true}
		]],
		pageNumber : 1,
		loadMsg : '数据加载中,请稍后...',
		pageList : [ 10, 30, 50, 100 ],
		onLoadError : function() {
			alert('数据加载失败!');
		},
		queryParams : {
		},
		onClickRow : function(rowIndex, rowData) {
			$('#salmfTable').datagrid('unselectRow', rowIndex);
		},
		onDblClickRow : function(rowIndex, rowData) {
			if(globalMaps.get('3') != undefined&&(meac=='N'||meac=='S')) {
				updateSalmfSet(rowData.mfac,rowData.mfad,rowData.mfae,rowData.mfaf,rowData.mfag,rowData.mfah,rowData.mfai,rowData.mfaj,rowData.mfak,rowData.mfal);
			}
	    }
	}).datagrid('acceptChanges');
}
function reloadTable() {
	$('#salmfTable').datagrid('reload');
	$('#salmfTable').datagrid('clearSelections');
}
function optFormater() {
	var update = '';
	if(globalMaps.get('4') != undefined && meac == 'N') {
		update += "<a href='#' onclick='deleteSalmemf(\""+meaa+"\",\""+meab+"\")' class='easyui-linkbutton' plain='true' iconCls='icon-cancel'>删除</a>";
	}
	if(globalMaps.get('5') != undefined && meac == 'N') {
		update += "<a href='#' onclick='auditSalmemf(\""+meaa+"\",\""+meab+"\",\"Y\")' class='easyui-linkbutton' plain='true' iconCls='icon-tip'>审核</a>";
	}
	if(globalMaps.get('6') != undefined && meac == 'Y') {
		update += "<a href='#' onclick='auditSalmemf(\""+meaa+"\",\""+meab+"\",\"C\")' class='easyui-linkbutton' plain='true' iconCls='icon-tip'>指定结束</a>";
		update += "<a href='#' onclick='auditSalmemf(\""+meaa+"\",\""+meab+"\",\"N\")' class='easyui-linkbutton' plain='true' iconCls='icon-tip'>撤销审核</a>";
	}
	if(globalMaps.get('6') != undefined && meac == 'C') {
		update += "<a href='#' onclick='auditSalmemf(\""+meaa+"\",\""+meab+"\",\"Y\")' class='easyui-linkbutton' plain='true' iconCls='icon-tip'>撤销结束</a>";
	}
	if(globalMaps.get('7') != undefined && meac == 'Y') {
		update += "<a href='#' onclick='auditSalmemf(\""+meaa+"\",\""+meab+"\",\"V\")' class='easyui-linkbutton' plain='true' iconCls='icon-tip'>作废</a>";
	}
	if(globalMaps.get('3') != undefined && meac == 'S') {
		update += "<a href='#' onclick='auditSalmemf(\""+meaa+"\",\""+meab+"\",\"N\")' class='easyui-linkbutton' plain='true' iconCls='icon-ok'>保存</a>";
	}
	$("#menuDiv").html(update);
	$.parser.parse($("#menuDiv"));
}
//----------------新增Begin
function setDialogAdd() {
	$('#salmfAdd').dialog({
		title : '新增单身',
		modal : true,
		left : 50,
		top : 50,
		collapsible : true,
		resizable : true
	});
}
function openDialogAdd() {
	addSalmfReset();
	$('#salmfAdd').dialog('open');
}
function closeDialogAdd() {
	addSalmfReset();
	$('#salmfAdd').dialog('close');
}
function salmfAdd() {
	var validateResult = $("#addForm").form('validate');
	if(validateResult == false) {
		return false;
	}
	$('#addForm').submit();
}
function addSalmfReset() {
	$("#salmfAddMfad").val("");
	$("#salmfAddMfae").val("");
	$("#salmfAddMfaf").val("");
	$("#salmfAddMfag").text("");
	$("#salmfAddMfah").text("");
	$("#salmfAddMfai").val("");
	$("#salmfAddMfaj").val("");
	$("#salmfAddMfak").val("");
	
}
//----------------新增End
//----------------更新Begin
function setDialogUpdate() {
	$('#salmfUpdate').dialog({
		title : '编辑',
		modal : true,
		left : 50,
		top : 50,
		collapsible : true,
		resizable : true
	});
}
function openDialogUpdate() {
	$('#salmfUpdate').dialog('open');
}
function closeDialogUpdate() {
	updateSalmfReset();
	$('#salmfUpdate').dialog('close');
}
function salmfUpdate() {
	var validateResult = $("#updateForm").form('validate');
	if(validateResult == false) {
		return false;
	}
	$('#updateForm').submit();
}
function updateSalmfReset() {
	$("#salmfUpdateMfad").val("");
	$("#salmfUpdateMfae").val("");
	$("#salmfUpdateMfaf").val("");
	$("#salmfUpdateMfag").text("");
	$("#salmfUpdateMfah").text("");
	$("#salmfUpdateMfai").val("");
	$("#salmfUpdateMfaj").val("");
	$("#salmfUpdateMfak").val("");
	$("#salmfUpdateMfal").val("");

}
function updateSalmfSet(mfac,mfad,mfae,mfaf,mfag,mfah,mfai,mfaj,mfak,mfal) {
	openDialogUpdate();
	$("#salmfUpdateMfac").val(mfac);
	$("#salmfUpdateMfad").val(mfad);
	$("#salmfUpdateMfae").val(mfae);
	$("#salmfUpdateMfaf").val(mfaf);
	$("#salmfUpdateMfag").text(mfag);
	$("#salmfUpdateMfah").text(mfah);
	$("#salmfUpdateMfai").val(mfai);
	$("#salmfUpdateMfaj").val(mfaj);
	$("#salmfUpdateMfak").val(mfak);
	$("#salmfUpdateMfal").val(mfal);
}
//----------------更新End
//----------------删除Begin
function delSalmf(){
	if ($('#salmfTable').datagrid('getSelected')) {
		var ids = [];
		var selectedRow = $('#salmfTable').datagrid('getSelections');
		for ( var i = 0; i < selectedRow.length; i++) {
			ids.push(selectedRow[i].mfac);
		}
		var mfac = ids.join(',');
		changeStatus('/wms/salmf/delSalmf.xhtml?mfac=' + mfac+'&mfaa='+meaa+'&mfab='+meab);
		$('#salmfTable').datagrid('clearSelections');
	}
}
function delSalmfAll(){
	changeStatus('/wms/salmf/delSalmfAll.xhtml?mfaa='+meaa+'&mfab='+meab);
}
//----------------删除End
//-------------SHENHE---
function auditSalmemf(meaa,meab,flag) {
	changeStatus('/wms/salmf/auditSalmf.xhtml?meaa=' + meaa + '&meab=' + meab+ '&flag=' + flag);
	window.parent.returnValue=1;
	window.close();

}
function deleteSalmemf(meaa,meab) {
	
	
	$.messager.confirm('删除提示', '你确定删除该采购单吗?', function(r) {
		if(r) {
			changeStatus('/wms/salmf/delSalmemf.xhtml?meaa=' + meaa + '&meab=' + meab);
			window.parent.returnValue=1;
			window.close();
		}
	});
}
function saveSalmemf(){
	var validateResult = $("#headForm").form('validate');
	if(validateResult == false) {
		return false;
	}
	$('#headForm').submit();
}
function changeStatus(url){
	$.ajax({
		async : false,
		cache : false,
		type : 'POST',
		dataType : "text",
		url : url,
		error : function () {
			alert('请求失败');
		},
		success : function(result){
			if(result=="success") {
//				alert("操作成功!");
			} else {
				alert(result);
			}
			reloadTable();
		}
	});
}
window.onbeforeunload = function(){  
	var n = window.event.screenX - window.screenLeft;   
    var b = n > document.documentElement.scrollWidth-20;   //		
      if((b&&window.event.clientY<0||window.event.altKey)&&meac=='S'){   
    	  delSalmfAll();
//          alert("是关闭而非刷新");   
//          window.event.returnValue   =   "是否关闭？";
      }else{
//             alert("是刷新而非关闭");   
     }  
 } ;

